package com.luban.moudle.register;

import com.luban.moudle.instance.InstanceInfo;
import com.luban.moudle.instance.Lease;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class InstanceRegister implements Register {
    /**
     * key: appName
     * value: server info
     */
    private ConcurrentHashMap<String, Map<String, Lease<InstanceInfo>>>
            register = RegisterServer.getIntance().getRegister();

    protected ConcurrentHashMap<String, Lease<ServerInfo>>
            server = RegisterServer.getIntance().getServer();

    @Override
    public void register(InstanceInfo info, int timeLimit, boolean isSync) {
        if (!isSync) {
            String appName = info.getInstanceName();
            String instanceId = info.getInstanceId();
            Map<String, Lease<InstanceInfo>> instanceInfoMap;
            Lease<InstanceInfo> lease = new Lease<InstanceInfo>(info, getlastActivityTime(), timeLimit);
            if (!register.containsKey(appName)) {
                instanceInfoMap = new HashMap<>();
                instanceInfoMap.put(instanceId, lease);
                register.put(info.getInstanceName(), instanceInfoMap);
            } else {
                instanceInfoMap = register.get(appName);
                instanceInfoMap.put(instanceId, lease);
                register.put(info.getInstanceName(), instanceInfoMap);
            }
        }
    }

    @Override
    public void renew(String appName, String instanceId, boolean isSync) {
        if (!isSync) {
            if (register.containsKey(appName)) {
                Map<String, Lease<InstanceInfo>> instanceInfoMap = register.get(appName).containsKey(instanceId) ? register.get(appName) : null;
                if (instanceInfoMap != null) {
                    Lease<InstanceInfo> lease = instanceInfoMap.get(instanceId);
                    if (!isNull(lease)) {
//                        lease.renew();
                        instanceInfoMap.put(instanceId, lease);
                        register.put(appName, instanceInfoMap);
                    }
                }
            }
        }
    }


    //取消注册需要判断服务的过期时间吗？
    @Override
    public void cancel(String appName, String instanceId, boolean isSync) {
        if (!isSync) {
            if (register.containsKey(appName)) {
                Map<String, Lease<InstanceInfo>> instanceInfoMap = register.get(appName).containsKey(instanceId) ? register.get(appName) : null;
                if (!isNull(instanceInfoMap)) {
                    if (instanceInfoMap.containsKey(instanceId)) {
                        instanceInfoMap.remove(instanceId);
                    }
                    if (instanceInfoMap.size() <= 0) {
                        instanceInfoMap = null;
                        register.remove(appName);
                    } else {
                        register.put(appName, instanceInfoMap);
                    }
                }
            }
        }
    }

    private synchronized long getlastActivityTime() {
        return System.currentTimeMillis();
    }

    private boolean isNull(Object obj) {
        return obj == null;
    }
}
